package com.ai.zhly.service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

import com.ai.dmc.index.base.impl.base.BaseServiceImpl;
import com.ai.dmc.index.base.impl.jotmBase.BaseMServiceImpl;
import com.ai.dmc.index.base.sequence.SequenceGenerator;
import com.ai.dmc.index.common.bean.AnalysisBean;
import com.ai.dmc.index.common.bean.AreaBean;
import com.ai.dmc.index.common.bean.CommonBaseBean;
import com.ai.dmc.index.common.bean.ExportBean;
import com.ai.dmc.index.common.cache.AreaCache;
import com.ai.dmc.index.common.exception.BusinessException;
import com.ai.dmc.index.common.util.BasicKMeans;
import com.ai.dmc.index.common.util.DateUtil;
import com.ai.dmc.utils.json.JsonUtil;
import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;
import com.ai.zhly.service.ISceneMonitorService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 
* @Title: 行为分析
* @Description: 行为分析service
* @author heyl  
* @date 2016年7月21日 下午4:22:54
* Copyright：Copyright (c) 2016
* Company：北京亚信智慧数据科技有限公司
* @version V1.0
 */
public class BehaviorServiceImpl extends BaseServiceImpl implements IBehaviorService{
	// 定义对应Mapper文件的命名空间
    private static final String NAMESPACE = "Behavior.";
    
    public static Logger log = LoggerFactory.getServiceLog(BehaviorServiceImpl.class);

    private SequenceGenerator sequenceGenerator;
    
    private ICommonService commonService;

    public void setSequenceGenerator(SequenceGenerator sequenceGenerator) {
        this.sequenceGenerator = sequenceGenerator;
    }
    

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}
	
	@Override
	public void queryBehaviorInfo(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		DecimalFormat    df   = new DecimalFormat("######0.00"); 
		//1.交通方式分析
		List<CommonBaseBean> rList = new ArrayList<CommonBaseBean>();
		try {			
			rList = this.getBaseDao().queryForList(
	                NAMESPACE + "queryTravelTypeInfo", inputObject.getParams(),CommonBaseBean.class);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		JSONArray travelTypeList = JSONArray.fromObject(rList);
		resultMap.put("travelTypeList", null == travelTypeList ? "" : travelTypeList);

		//2.app分类分析
		List<String> AppTypeNameList = new ArrayList<String>();
		List<String> AppTypeValueList = new ArrayList<String>();
		List<CommonBaseBean> List2 = new ArrayList<CommonBaseBean>();
		try {			
			List2 = this.getBaseDao().queryForList(
	                NAMESPACE + "queryAppTypeInfo", inputObject.getParams(),CommonBaseBean.class);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		//JSONArray AppTypeList = JSONArray.fromObject(List2);
		//查询TOP5
		for(int i=0;i<List2.size()&&i<5;i++){
			CommonBaseBean appTypeBean = List2.get(i);
		
		if(appTypeBean == null) {
			break;
		} else {
			AppTypeNameList.add(appTypeBean.getQuery_value1());			
			AppTypeValueList.add(df.format(Double.parseDouble(appTypeBean.getQuery_value2())/10000));
			}
		}
        resultMap.put("AppTypeNameList", null == AppTypeNameList ? "" : AppTypeNameList);
        resultMap.put("AppTypeValueList", null == AppTypeValueList ? "" : AppTypeValueList);
        
        //3.APP分析
        List<String> AppNameList = new ArrayList<String>();
		List<String> AppValueList = new ArrayList<String>();
		List<CommonBaseBean> List3 = new ArrayList<CommonBaseBean>();
		try {			
			List3 = this.getBaseDao().queryForList(
	                NAMESPACE + "queryAppInfo", inputObject.getParams(),CommonBaseBean.class);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		//JSONArray AppTypeList = JSONArray.fromObject(List2);
		//查询TOP5
		for(int i=0;i<List3.size()&&i<5;i++){
			CommonBaseBean appBean = List3.get(i);
		
		if(appBean == null) {
			break;
		} else {
			AppNameList.add(appBean.getQuery_value1());			
			AppValueList.add(df.format(Double.parseDouble(appBean.getQuery_value2())/10000));
			}
		}
        resultMap.put("AppNameList", null == AppNameList ? "" : AppNameList);
        resultMap.put("AppValueList", null == AppValueList ? "" : AppValueList);
      //4.网站访问分析
        List<String> WebNameList = new ArrayList<String>();
		List<String> WebValueList = new ArrayList<String>();
		List<CommonBaseBean> List4 = new ArrayList<CommonBaseBean>();
		try {			
			List4 = this.getBaseDao().queryForList(
	                NAMESPACE + "queryWebAccessInfo", inputObject.getParams(),CommonBaseBean.class);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		//JSONArray AppTypeList = JSONArray.fromObject(List2);
		//查询TOP5
		for(int i=0;i<List4.size()&&i<5;i++){
			CommonBaseBean webBean = List4.get(i);
		
		if(webBean == null) {
			break;
		} else {
			WebNameList.add(webBean.getQuery_value1());			
			WebValueList.add(df.format(Double.parseDouble(webBean.getQuery_value2())/10000));
			}
		}
        resultMap.put("WebNameList", null == WebNameList ? "" : WebNameList);
        resultMap.put("WebValueList", null == WebValueList ? "" : WebValueList);
        
        outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}
	
	@Override
	public void querySurfKeywordInfo(InputObject inputObject, OutputObject outputObject) throws BusinessException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		//上网搜索分析
		List<String> searchTypeList = new ArrayList<String>();
		List<String> uniqueTypeList = new ArrayList<String>();
		
        List<String> searchNameList0 = new ArrayList<String>();
		List<String> searchValueList0 = new ArrayList<String>();
        List<String> searchNameList1 = new ArrayList<String>();
		List<String> searchValueList1 = new ArrayList<String>();
        List<String> searchNameList2 = new ArrayList<String>();
		List<String> searchValueList2 = new ArrayList<String>();
        List<String> searchNameList3 = new ArrayList<String>();
		List<String> searchValueList3 = new ArrayList<String>();
        List<String> searchNameList4 = new ArrayList<String>();
		List<String> searchValueList4 = new ArrayList<String>();
        List<String> searchNameList5 = new ArrayList<String>();
		List<String> searchValueList5 = new ArrayList<String>();
		
		List<CommonBaseBean> searchList = new ArrayList<CommonBaseBean>();
		try {			
			searchList = this.getBaseDao().queryForList(
	                NAMESPACE + "querySurfKeyInfo", inputObject.getParams(),CommonBaseBean.class);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		//获取搜索分类
		for(int i=0;i<searchList.size();i++){
			searchTypeList.add(searchList.get(i).getQuery_value3());
		}
	    for(String s:searchTypeList){  
	        if((!uniqueTypeList.contains(s)) && s!="" && s!=null){  
	        	uniqueTypeList.add(s);  
	        }  
	    }  
		
	  	//获取各分类下搜索信息
		for(int i=0;i<searchList.size();i++){
			CommonBaseBean cBean = searchList.get(i);
		
			if(cBean == null) {
				break;
			} else {
				String b=cBean.getQuery_value3();
				if( uniqueTypeList.size()>0 ){
				if(uniqueTypeList.get(0).equals(b) && searchNameList0.size()<10){
					searchNameList0.add(cBean.getQuery_value1());
					searchValueList0.add(cBean.getQuery_value2());
				}
				}
				if(uniqueTypeList.size()>1 ){
				if(uniqueTypeList.get(1).equals(b) && searchNameList1.size()<10){
					searchNameList1.add(cBean.getQuery_value1());
					searchValueList1.add(cBean.getQuery_value2());
				}
				}
				if(uniqueTypeList.size()>2 ){
				if(uniqueTypeList.get(2).equals(b) && searchNameList2.size()<10){
					searchNameList2.add(cBean.getQuery_value1());
					searchValueList2.add(cBean.getQuery_value2());
				}
				}
				if(uniqueTypeList.size()>3 ){
				if(uniqueTypeList.get(3).equals(b) && searchNameList3.size()<10){
					searchNameList3.add(cBean.getQuery_value1());
					searchValueList3.add(cBean.getQuery_value2());
				}
				}
				if(uniqueTypeList.size()>4 ){
				if(uniqueTypeList.get(4).equals(b) && searchNameList4.size()<10){
					searchNameList4.add(cBean.getQuery_value1());
					searchValueList4.add(cBean.getQuery_value2());
				}
				}
				if(uniqueTypeList.size()>5 ){
				if(uniqueTypeList.get(5).equals(b) && searchNameList5.size()<10){
					searchNameList5.add(cBean.getQuery_value1());
					searchValueList5.add(cBean.getQuery_value2());
				}
				}
				
			}
		}

		resultMap.put("uniqueTypeList", null == uniqueTypeList ? "" : uniqueTypeList);
		
        resultMap.put("searchNameList0", null == searchNameList0 ? "" : searchNameList0);
        resultMap.put("searchNameList1", null == searchNameList1 ? "" : searchNameList1);
        resultMap.put("searchNameList2", null == searchNameList2 ? "" : searchNameList2);
        resultMap.put("searchNameList3", null == searchNameList3 ? "" : searchNameList3);
        resultMap.put("searchNameList4", null == searchNameList4 ? "" : searchNameList4);
        resultMap.put("searchNameList5", null == searchNameList5 ? "" : searchNameList5);
        
        resultMap.put("searchValueList0", null == searchValueList0 ? "" : searchValueList0);
        resultMap.put("searchValueList1", null == searchValueList1 ? "" : searchValueList1);
        resultMap.put("searchValueList2", null == searchValueList2 ? "" : searchValueList2);
        resultMap.put("searchValueList3", null == searchValueList3 ? "" : searchValueList3);
        resultMap.put("searchValueList4", null == searchValueList4 ? "" : searchValueList4);
        resultMap.put("searchValueList5", null == searchValueList5 ? "" : searchValueList5);

//		//2.上网搜索分析-住
//        List<String> liveNameList = new ArrayList<String>();
//		List<String> liveValueList = new ArrayList<String>();
//		List<CommonBaseBean> liveList = new ArrayList<CommonBaseBean>();
//		try {			
//			liveList = this.getBaseDao().queryForList(
//	                NAMESPACE + "querySurfLiveInfo", inputObject.getParams(),CommonBaseBean.class);
//		} catch (Exception e) {
//			System.out.println(e.getMessage());
//		}
//		//查询TOP10
//		for(int i=0;i<liveList.size()&&i<10;i++){
//			CommonBaseBean liveBean = liveList.get(i);
//		
//		if(liveBean == null) {
//			break;
//		} else {
//			liveNameList.add(liveBean.getQuery_value1());
//			liveValueList.add(liveBean.getQuery_value2());
//			}
//		}
//        resultMap.put("liveNameList", null == liveNameList ? "" : liveNameList);
//        resultMap.put("liveValueList", null == liveValueList ? "" : liveValueList);
//
//		//3.上网搜索分析-行
//        List<String> goNameList = new ArrayList<String>();
//		List<String> goValueList = new ArrayList<String>();
//		List<CommonBaseBean> goList = new ArrayList<CommonBaseBean>();
//		try {			
//			goList = this.getBaseDao().queryForList(
//	                NAMESPACE + "querySurfGoInfo", inputObject.getParams(),CommonBaseBean.class);
//		} catch (Exception e) {
//			System.out.println(e.getMessage());
//		}
//		//查询TOP10
//		for(int i=0;i<goList.size()&&i<10;i++){
//			CommonBaseBean goBean = goList.get(i);
//		
//		if(goBean == null) {
//			break;
//		} else {
//			goNameList.add(goBean.getQuery_value1());
//			goValueList.add(goBean.getQuery_value2());
//			}
//		}
//        resultMap.put("goNameList", null == goNameList ? "" : goNameList);
//        resultMap.put("goValueList", null == goValueList ? "" : goValueList);
//        
//      //4.上网搜索分析-游
//        List<String> travelNameList = new ArrayList<String>();
//		List<String> travelValueList = new ArrayList<String>();
//		List<CommonBaseBean> travelList = new ArrayList<CommonBaseBean>();
//		try {			
//			travelList = this.getBaseDao().queryForList(
//	                NAMESPACE + "querySurfTravelInfo", inputObject.getParams(),CommonBaseBean.class);
//		} catch (Exception e) {
//			System.out.println(e.getMessage());
//		}
//		//查询TOP10
//		for(int i=0;i<travelList.size()&&i<10;i++){
//			CommonBaseBean travelBean = travelList.get(i);
//		
//		if(travelBean == null) {
//			break;
//		} else {
//			travelNameList.add(travelBean.getQuery_value1());
//			travelValueList.add(travelBean.getQuery_value2());
//			}
//		}
//        resultMap.put("travelNameList", null == travelNameList ? "" : travelNameList);
//        resultMap.put("travelValueList", null == travelValueList ? "" : travelValueList);
//      //5.上网搜索分析-购
//        List<String> buyNameList = new ArrayList<String>();
//		List<String> buyValueList = new ArrayList<String>();
//		List<CommonBaseBean> buyList = new ArrayList<CommonBaseBean>();
//		try {			
//			buyList = this.getBaseDao().queryForList(
//	                NAMESPACE + "querySurfBuyInfo", inputObject.getParams(),CommonBaseBean.class);
//		} catch (Exception e) {
//			System.out.println(e.getMessage());
//		}
//		//查询TOP10
//		for(int i=0;i<buyList.size()&&i<10;i++){
//			CommonBaseBean buyBean = buyList.get(i);
//		
//		if(buyBean == null) {
//			break;
//		} else {
//			buyNameList.add(buyBean.getQuery_value1());
//			buyValueList.add(buyBean.getQuery_value2());
//			}
//		}
//        resultMap.put("buyNameList", null == buyNameList ? "" : buyNameList);
//        resultMap.put("buyValueList", null == buyValueList ? "" : buyValueList);
//        
//      //6.上网搜索分析-娱
//        List<String> joyNameList = new ArrayList<String>();
//		List<String> joyValueList = new ArrayList<String>();
//		List<CommonBaseBean> joyList = new ArrayList<CommonBaseBean>();
//		try {			
//			joyList = this.getBaseDao().queryForList(
//	                NAMESPACE + "querySurfJoyInfo", inputObject.getParams(),CommonBaseBean.class);
//		} catch (Exception e) {
//			System.out.println(e.getMessage());
//		}
//		//查询TOP10
//		for(int i=0;i<joyList.size()&&i<10;i++){
//			CommonBaseBean joyBean = joyList.get(i);
//		
//		if(joyBean == null) {
//			break;
//		} else {
//			joyNameList.add(joyBean.getQuery_value1());
//			joyValueList.add(joyBean.getQuery_value2());
//			}
//		}
//        resultMap.put("joyNameList", null == joyNameList ? "" : joyNameList);
//        resultMap.put("joyValueList", null == joyValueList ? "" : joyValueList);
//        
        
        outputObject.setObject(JsonUtil.Object2Json(resultMap));
	}
	
}
